var blnUserIsAdmin = false;
var remoteAuthUrl = "/index.cfm?fuseaction=fusecache.remoteAuth";
var remoteKillUrl = "/index.cfm?fuseaction=fusecache.killKeys";

/*
	$(document).ready()
	When the document has loaded, confirm that the $.cookies plugin is
	available.  If so, fire off the authenticateUser() function.
*/
$(document).ready(function() {			   
	if (jQuery.cookies) {
		authenticateUser();
	}
});

/*
	authenticateUser()
	This is the function that kicks it all off.  It checks for the cookie.
*/
function authenticateUser() {
	// Use the jQuery cookie plugin to check for the presence of the
	// FCACHEADMIN cookie.
	if ($.cookies.get("FCACHEADMIN") != null) {
		/*
			We found the cookie, now we need to compare its value to the hash
			of the value stored in the Fusecache configuration.  We do this by
			making an AJAX call to the Fusecache utility circuit.  If the 
			remote authorization returns true, examineDocument() is run.
		*/
		$.ajax({
			url: remoteAuthUrl,
			dataType: "json",
			data: { cookie: $.cookies.get("FCACHEADMIN") },
			success: function(results){
				if (results.response == "true") {
					blnUserIsAdmin = true;
					examineDocument();
				}
			}
		});
	}
}


/*
	examineDocument()
	This function is called by the authenticateUser() function after a user
	has successfully been authenticated.  It uses jQuery selectors to find
	the debug text appended to a cached page.  If found, it in turn calls
	the showBar() function which actually inserts the necessary markup for the
	controls.
*/
function examineDocument() {
	var fusecacheDiv = $("#fusecache");
	if (fusecacheDiv.length != 0) {
		showBar(fusecacheDiv);
	}
}

/*
	showBar()
	This function is called by the examineDocument() function if it finds
	the Fusecache debug text / markings. It generates the HTML markup to display 
	the controls, and binds an onclick action to the links that fires of the cache
	clearing processes.
*/
function showBar(fusecacheDiv) {
	// Set url, then  write the bar markup and append it to the page
	var sMarkupOutput = "<div id='fusecacheInline'>" + fusecacheDiv.attr("class") + " &nbsp;&nbsp;  <a href='#'>CLEAR</a></div>";
	$("body").prepend(sMarkupOutput);
	
	// Now the the new amrkup is on the page. Select the link inside the control bar and apply 
	// a click action to it.
	var barLink = $("#fusecacheInline a");
	barLink.click(function(){
		
		// Make an AJAX call to the remote kill url, passing it the
		// necessary information to reauthenticate, etc.
		$.ajax({
			url: remoteKillUrl,
			dataType: "json",
			data: {
				key: fusecacheDiv.attr("class"),
				pw: $.cookies.get('FCACHEADMIN')
			},
		});
		
		// At this time we are not returning any feedback information, so we're
		// just going to fade the control out and reload the page.  Yes, it's kind of 
		// silly to fade when you're going to reload, but I think it looks good.
		$('#fusecacheInline').fadeOut('normal', function() {
			location.reload(); 
		});
		
		return false;
	});
}